<template>
  <div>
    <!-- action是必须属性
    list-type决定显示样式
   -->
    <el-upload
      action="#"
      list-type="picture-card"
      :file-list="fileList"
      :class="{disabled:fileList.length===1}"
      :on-preview="onPreview"
      :on-remove="onRemove"
      :on-change="onChange"
      :before-upload="befroeUpload"
      :http-request="upload"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <!-- 放置一个进度条 -->
    <el-progress v-if="showPercent" style="width:150px" :percentage="percent" />
    <el-dialog title="预览图片" :visible="showDialog" @close="showDialog=false">
      <img :src="imgURL" alt="">
    </el-dialog>
  </div>
</template>

<script>
// 引入腾讯云的库
import COS from 'cos-js-sdk-v5'
// 创建实例并验证身份
const cos = new COS({
  SecretId: 'AKIDay7zh8QSDivtb1A3f4D5h39zauxjFUTB',
  SecretKey: 'SdYCZgV3KL5LK2TdW8TXJq77qHSN1OVL'
})
export default {
  data() {
    return {
      fileList: [],
      showDialog: false,
      imgURL: '',
      percent: 0, // 默认百分比
      showPercent: false, // 控制进度条显示隐藏
      currentFileUid: ''
    }
  },
  methods: {
    onPreview(file) {
      console.log(file)
      this.imgURL = file.url
      this.showDialog = true
    },
    // 删除图片钩子函数
    onRemove(file, newFileList) {
      this.fileList = newFileList
    },
    // 图片改变时钩子函数
    onChange(file, newFileList) {
      this.fileList = newFileList
    },

    befroeUpload(file) {
      const maxSize = 5 * 1024 * 1024
      if (file.size > maxSize) {
        this.$message.warning('上传文件不能大于5M')
        return false
      }
      const typeAllowed = ['image/jpeg', 'image/png', 'image/bmp', 'image/gif']
      if (!typeAllowed.includes(file.type)) {
        this.$message.warning('只能上传jpeg/png格式的图片')
        return false
      }
      // 显示百分比
      this.showPercent = true
      //   记录文件uid,上传文件时校验文件上传成功与否
      this.currentFileUid = file.uid
    },
    // 上传图片的钩子函数
    upload(params) {
      console.log(params)
      cos.putObject({
        // 储存桶名称
        Bucket: 'syf-1307764400',
        // 地区代码
        Region: 'ap-guangzhou',
        // 储存后的文件名
        Key: params.file.name,
        // 默认标准储存不用修改
        StorageClass: 'STANDARD',
        // 文件对象本身
        Body: params.file,
        // 进度条函数
        onProgress: (progressData) => {
          console.log(92, JSON.stringify(progressData))
          this.percent = progressData.percent * 100
        }
      }, (err, data) => {
        // console.log(90, err)
        console.log(data)
        if (!err) {
          // 每当上传完毕,将腾讯云回显的地址放入文件列表中
        //   this.fileList[0].url = 'http://' + data.Location
        // 校验文件是否完成上传,如果进度条没有100%就点击保存会导致没完成上传
          this.fileList = this.fileList.map(item => {
            //   如果遍历fileList的item的uid等于当前的文件uid,则表示完成上传,添加upload属性
            if (item.uid === this.currentFileUid) {
              return { url: 'http://' + data.Location, upload: true }// 给当前上传图片添加属性upload
            }
            return item
          })
          this.fileList[0].status = 'success'
          setTimeout(() => {
            this.showPercent = false
            this.percent = 0
          }, 1000)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
    .disabled{
        ::v-deep .el-upload--picture-card{
            display: none;
        }
    }
     img{
            width: 100%;
        }
</style>
